package com.renne.email.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.renne.common.core.controller.BaseController;
import com.renne.common.core.domain.AjaxResult;
import com.renne.common.core.page.TableDataInfo;
import com.renne.common.annotation.Log;
import com.renne.common.enums.BusinessType;

import com.renne.email.domain.MailImportHistoryQuery;
import com.renne.email.domain.MailImportHistoryVo;
import com.renne.email.service.IMailImportHistoryService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 邮件导入历史记录Controller
 *
 * @author LiuYu
 * @date 2024-07-26
 */
@Tag(name = "mail-import-history-controller", description = "邮件导入历史记录")
@RestController
@RequestMapping("/email/import/history")
public class MailImportHistoryController extends BaseController {

    @Autowired
    private IMailImportHistoryService mailImportHistoryService;


    /**
     * 查询邮件导入历史记录列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询邮件导入历史记录列表")
    public TableDataInfo list(MailImportHistoryQuery query) {
        startPage();
        List<MailImportHistoryVo> list = mailImportHistoryService.getMailImportHistoryVoList(query);
        return getDataTable(list);
    }

    /**
     * 获取邮件导入历史记录详细信息
     */
    @GetMapping("/{mailImportHistoryId}")
    @Operation(summary = "查询邮件导入历史记录详细")
    public AjaxResult getById(@Parameter(description = "主键ID") @PathVariable("mailImportHistoryId") String mailImportHistoryId) {
        return success(mailImportHistoryService.getMailImportHistoryVoById(mailImportHistoryId));
    }

    /**
     * 新增邮件导入历史记录
     */
    @PostMapping
    @Log(title = "邮件导入历史记录", businessType = BusinessType.INSERT)
    @Operation(summary = "新增邮件导入历史记录")
    public AjaxResult add(@Validated @RequestBody MailImportHistoryVo mailImportHistoryVo) {
        return toAjax(mailImportHistoryService.saveMailImportHistoryVo(mailImportHistoryVo));
    }

    /**
     * 修改邮件导入历史记录
     */
    @PutMapping
    @Log(title = "邮件导入历史记录", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改邮件导入历史记录")
    public AjaxResult update(@Validated @RequestBody MailImportHistoryVo mailImportHistoryVo) {
        return toAjax(mailImportHistoryService.updateMailImportHistoryVo(mailImportHistoryVo));
    }

    /**
     * 删除邮件导入历史记录记录
     */
    @DeleteMapping("/{mailImportHistoryId}")
    @Log(title = "邮件导入历史记录", businessType = BusinessType.DELETE)
    @Operation(summary = "删除邮件导入历史记录")
    public AjaxResult delete(@Parameter(description = "主键ID") @PathVariable("mailImportHistoryId") String[] mailImportHistoryId) {
        return toAjax(mailImportHistoryService.deleteMailImportHistoryByIds(mailImportHistoryId));
    }

    /**
     * 导出邮件导入历史记录
     */
    @Log(title = "邮件导入历史记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        Map<String, Object> map = mailImportHistoryService.importMailHistory(file);
        return success(map);
    }

    /**
     * 根据邮件导入记录ID发送邮件
     */
    @PostMapping("/sendEmail/{mailImportHistoryId}")
    @Log(title = "邮件导入历史记录", businessType = BusinessType.UPDATE)
    @Operation(summary = "根据邮件导入记录ID发送邮件")
    public AjaxResult sendEmail(@Parameter(description = "主键ID") @PathVariable("mailImportHistoryId") String[] mailImportHistoryId) {
        mailImportHistoryService.sendMailByImportHistoryIds(mailImportHistoryId);
        return AjaxResult.success();
    }

    /**
     * 导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response, HttpServletRequest request) {
        try {
            mailImportHistoryService.importTemplate(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
